package com.loongtech.bi.manager.report;

import com.loongtech.bi.action.UIHelper;
import com.loongtech.bi.action.birealtime.GameOverviewAction;
import com.loongtech.bi.action.newsystem.SystemConfig;
import com.loongtech.bi.entity.realTime.EntityDauByDay;
import com.loongtech.bi.manager.pay.NewUserAddCashUserManager;
import com.loongtech.bi.manager.pay.TotalCashManager;
import com.loongtech.bi.manager.player.ActiveDeviceManager;
import com.loongtech.bi.manager.player.NewUserManager;
import com.loongtech.bi.manager.system.ConfigManager;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.NumberUtil;
import com.loongtech.core.util.Time;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class GameOverviewManager extends ManagerQueryCacheBase<EntityDauByDay> {
    private static final long serialVersionUID = 1L;
    @Resource(name = "systemConfig")
    private SystemConfig systemConfig;
    @Resource
    private ActiveDeviceManager activeDeviceManager;
    @Resource
    private NewUserManager newUserManager;
    @Resource
    private NewUserAddCashUserManager newUserAddCashUserManager;
    @Resource
    private TotalCashManager totalCashManager;
    @Resource
    private ConfigManager configManager;

    public List<Object> getTotalDatas(String platformid) {
        String opendate = configManager.getConfigValueByName(ConfigManager.OPEN_DATE) == null ? "1900-01-01" : configManager.getConfigValueByName(ConfigManager.OPEN_DATE);
        Date startDate = Time.formatDate(opendate, "yyyy-MM-dd");
        Date endDate = new Date(Time.currentTime() - Time.DAY);
        List<Object> list = new ArrayList<>();
        // 开服至今激活设备
        Long num;
        if ("0".equals(platformid)) {
            num = activeDeviceManager.getActiveDevicesFromOpenDate(startDate, endDate);
        } else {
            num = activeDeviceManager.getActiveDevicesFromOpenDateByPlatform(startDate, endDate, platformid);
        }
        list.add(num);
        // 开服至今新增用户
        if ("0".equals(platformid)) {
            num = newUserManager.getNewUsersByDateFrommOpenDate(startDate, endDate);
        } else {
            num = newUserManager.getNewUsersByDateFrommOpenDateByPlatform(startDate, endDate, platformid);
        }
        list.add(num);
        // 开服至今付费玩家
        if ("0".equals(platformid)) {
            num = newUserAddCashUserManager.getNewUserAddCashUsersFromOpenDate(startDate, endDate);
        } else {
            num = newUserAddCashUserManager.getNewUserAddCashUsersFromOpenDateByPlatform(startDate, endDate, platformid);
        }
        list.add(num);
        // 开服至今付费金额
        if ("0".equals(platformid)) {
            num = totalCashManager.getTotalCashFromOpenDate(startDate, endDate);
        } else {
            num = totalCashManager.getTotalCashFromOpenDateByPlatform(startDate, endDate, platformid);
        }
        list.add(num);
        return list;
    }

    public Map<String, Map<String, String>> getTotalDatas2(Date startDate) {
        Map<String, Map<String, String>> result = new HashMap<String, Map<String, String>>(){{
            put("0", new HashMap<String, String>() {{
                put("deviceActive", "0");
                put("newAddPlayer", "0");
                put("activePlayer", "0");
                put("payPlayer", "0");
                put("payAmount", "0");
            }});
            put("2", new HashMap<String, String>() {{
                put("deviceActive", "0");
                put("newAddPlayer", "0");
                put("activePlayer", "0");
                put("payPlayer", "0");
                put("payAmount", "0");
            }});
            put("3", new HashMap<String, String>() {{
                put("deviceActive", "0");
                put("newAddPlayer", "0");
                put("activePlayer", "0");
                put("payPlayer", "0");
                put("payAmount", "0");
            }});
        }};

        Date endDate = new Date(Time.currentTime() - Time.DAY);

        // 开服至今激活设备
        List<Object[]> res = activeDeviceManager.getActiveDevicesFromOpenDate2(startDate, endDate);
        if (res != null && res.size() > 0 && res.get(0) != null) {
            for (Object[] o : res) {
                String platformid = String.valueOf(o[0]);
                Map<String, String> tmp = result.get(platformid);
                if (tmp != null) {
                    tmp.put("deviceActive", GameOverviewAction.format.format(NumberUtil.parseLong(String.valueOf(o[1]))));
                }
            }
        }

        // 开服至今新增用户
        res = newUserManager.getNewUsersByDateFrommOpenDate2(startDate, endDate);
        if (res != null && res.size() > 0 && res.get(0) != null) {
            for (Object[] o : res) {
                String platformid = String.valueOf(o[0]);
                Map<String, String> tmp = result.get(platformid);
                if (tmp != null) {
                    String value = GameOverviewAction.format.format(NumberUtil.parseLong(String.valueOf(o[1])));
                    tmp.put("newAddPlayer", value);
                    tmp.put("activePlayer", value);
                }
            }
        }

        // 开服至今付费玩家
        res = newUserAddCashUserManager.getNewUserAddCashUsersFromOpenDate2(startDate, endDate);
        if (res != null && res.size() > 0 && res.get(0) != null) {
            for (Object[] o : res) {
                String platformid = String.valueOf(o[0]);
                Map<String, String> tmp = result.get(platformid);
                if (tmp != null) {
                    tmp.put("payPlayer", GameOverviewAction.format.format(NumberUtil.parseLong(String.valueOf(o[1]))));
                }
            }
        }

        // 开服至今付费金额
        res = totalCashManager.getTotalCashFromOpenDate2(startDate, endDate);
        if (res != null && res.size() > 0 && res.get(0) != null) {
            for (Object[] o : res) {
                String platformid = String.valueOf(o[0]);
                Map<String, String> tmp = result.get(platformid);
                if (tmp != null) {
                    tmp.put("payAmount", GameOverviewAction.format.format(UIHelper.getYuan(NumberUtil.parseLong(String.valueOf(o[1])))));
                }
            }
        }

        return result;
    }
}
